package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateService strategyTemplateService;

    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        model.addAttribute("commends",commends);
        //hotCds
        List<StrategyStatisVO> hotCds = strategyStatisVOService.queryHotCommendTopTen();
        model.addAttribute("hotCds",hotCds);
        //abroadCds
        List<StrategyStatisVO> abroadCds = strategyStatisVOService.queryAbroadCommendTopTen();
        model.addAttribute("abroadCds",abroadCds);
        //unabroadCds
        List<StrategyStatisVO> unabroadCds = strategyStatisVOService.queryChinaCommendTopTen();
        model.addAttribute("unabroadCds",unabroadCds);
        //themeCds
        List<Map<String,Object>> themeCds = strategyTemplateService.queryThemeCommend();
        model.addAttribute("themeCds",themeCds);
        //chinas
        List<StatisVO> chinas = strategyTemplateService.queryConditionChina();
        model.addAttribute("chinas",chinas);
        //abroads
        List<StatisVO> abroads = strategyTemplateService.queryConditionAbroads();
        model.addAttribute("abroads",abroads);
        //themes
        List<StatisVO> themes = strategyTemplateService.queryConditionThemes();
        model.addAttribute("themes",themes);
        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, StrategySearchQuery qo){
        Page page = strategyTemplateService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //dest
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",destination);
        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategy/list";
    }

    @RequestMapping("/detail")
    public String detail(Model model,Long id){
        StrategyDetail strategyDetail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        strategyDetail.setStrategyContent(content);
        model.addAttribute("detail",strategyDetail);
        //数据统计，阅读数加一
        strategyStatisVOService.increaseViewNum(id,1);
        strategyStatisVOService.addHotCommendScore(id,1);
        model.addAttribute("vo",strategyStatisVOService.getStrategyStatisVO(id));
        return "strategy/detail";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);
        strategyStatisVOService.increaseReplyNum(comment.getDetailId(),1);
        strategyStatisVOService.addHotCommendScore(comment.getDetailId(),1);
        return AjaxResult.SUCCESS.addData(strategyStatisVOService.getStrategyStatisVO(comment.getDetailId()).getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo,@UserParam UserInfo userInfo){
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        //model.addAttribute("userInfo",userInfo);
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        //如果没有登录，提示用户先登录
        if (userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        boolean ret = strategyStatisVOService.favor(sid,userInfo.getId());
        strategyStatisVOService.addAbroadAndChinaScore(sid,ret?1:-1);
        return new AjaxResult(ret,"").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        //如果没有登录，提示用户先登录
        if (userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        boolean ret = strategyStatisVOService.strategyThumbup(sid,userInfo.getId());
        if(ret){
            strategyStatisVOService.addAbroadAndChinaScore(sid,1);
        }
        return new AjaxResult(ret,"").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }
    //攻略分享
    @RequestMapping("/share")
    @ResponseBody
    public Object share(Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result =  new AjaxResult(false,"请先登陆噢亲~~");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return  result;
        }
        //可以转发多次
        strategyStatisVOService.sharenumIncrease(sid,1);
        return AjaxResult.SUCCESS.addData(strategyStatisVOService.getStrategyStatisVO(sid));

    }
}
